import java.util.HashMap;
import java.util.Map;

public class Solution {
    public boolean wordPattern(String pattern, String str) {
        Map<String, Character> str2ch = new HashMap<>();
        Map<Character, String> ch2str = new HashMap<>();
        String[] words = str.split(" "); // 将字符串按空格分割成单词数组
        if (pattern.length() != words.length) {
            return false; // 如果pattern的长度和单词数组的长度不一致，直接返回false
        }

        for (int i = 0; i < pattern.length(); i++) {
            char ch = pattern.charAt(i);
            String word = words[i];
            if (str2ch.containsKey(word) && str2ch.get(word) != ch) {
                return false; // 如果当前单词已经映射到其他字符，返回false
            }
            if (ch2str.containsKey(ch) && !ch2str.get(ch).equals(word)) {
                return false; // 如果当前字符已经映射到其他单词，返回false
            }
            str2ch.put(word, ch); // 建立单词到字符的映射
            ch2str.put(ch, word); // 建立字符到单词的映射
        }
        return true; // 如果所有映射都一致，返回true
    }

    public static void main(String[] args) {
        Solution solution = new Solution();
        String pattern1 = "abba";
        String str1 = "dog cat cat dog";
        System.out.println(solution.wordPattern(pattern1, str1));
    }
}